package com.mock.ems.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;
import com.mock.ems.config.UserManager;
import com.mock.ems.utils.TGlCateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金明细
 * @author whx
 * @date 2017/08/19
 */
public class ShopController extends Controller {
    public void index()  {
        renderJsp("/shop_list.jsp");
    }

    public void pagination() {
        int limit = getParaToInt("limit");
        int offset = getParaToInt("offset");
        int pageNumber = (offset / limit) + 1;

        String selectSql = "select * ";
        StringBuilder sqlExceptSql = new StringBuilder(" from t_ou_shop a inner join t_prod_rec b on a.id_ = b.rec_id_ where status_ = ?");
        List<Object> params = new ArrayList<Object>();
        params.add("audit_succeed");

        String nickName = getPara("nick_name", "");
        if (StringUtil.isNotEmpty(nickName.trim())) {
            sqlExceptSql.append( " and nick_name like ?");
            params.add("%" + nickName.trim() + "%");
        }

        String storeName = getPara("store_name_", "");
        if (StringUtil.isNotEmpty(storeName.trim())) {
            sqlExceptSql.append( " and store_name_ like ?");
            params.add("%" + storeName.trim() + "%");
        }

        String contact = getPara("contact_", "");
        if (StringUtil.isNotEmpty(contact.trim())) {
            sqlExceptSql.append( " and contact_ = ?");
            params.add(contact.trim());
        }

        String type = getPara("type_", "");
        if (StringUtil.isNotEmpty(type.trim())) {
            sqlExceptSql.append( " and type_ = ?");
            params.add(type.trim());
        }

        Page<Record> recordList = Db.paginate(pageNumber, limit, selectSql, sqlExceptSql.toString(), params.toArray());

        if(CollectionUtil.isNotEmpty(recordList.getList())){
            recordList.getList().stream().forEach(item->{
                Long userId = Long.parseLong(item.getStr("party_id_"));
                Record user = UserManager.getUser(userId);
                if(user == null){
                    user = Db.findById("user",userId);
                    if(user != null){
                        UserManager.setUser(user);
                    }
                }
                item.set("nick_name",user == null ? "未知" : user.getStr("nick_name"));

                Record recommend = Db.findFirst("select id_ from t_prod_rec where rec_id_ = ? and src_type_ = ?"
                        ,item.getStr("id_"),"indexShop");
                if(recommend != null){
                    item.set("recommend","Y");
                }else{
                    item.set("recommend","N");
                }
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

    @Before(Tx.class)
    public void recommend(){

        String shopIdList = getPara("shopIdList");

        if(StringUtil.isEmpty(shopIdList)){
            renderJson(new JsonResult().status("001").show("未选择商家，请联系管理员"));
            return;
        }

        String[] shopIdArr = shopIdList.split(",");
        for(String id : shopIdArr){
            Record record = Db.findFirst("select id_ from t_prod_rec where rec_id_ = ? and src_type_ = ?"
                    ,id,"indexShop");
            if(record == null){
                record = new Record();
                Long id_ = TGlCateUtil.getId();
                record.set("rec_id_",id).set("src_type_","indexShop")
                        .set("sort_",1).set("create_time_", DateUtil.getDate()).set("id_",id_);
                Db.save("t_prod_rec","id_",record);
            }
        }

        renderJson(new JsonResult().status("000").show("推荐成功"));
    }

    @Before(Tx.class)
    public void unrecommend(){

        String shopIdList = getPara("shopIdList");

        if(StringUtil.isEmpty(shopIdList)){
            renderJson(new JsonResult().status("001").show("未选择商家，请联系管理员"));
            return;
        }

        String[] shopIdArr = shopIdList.split(",");
        for(String id : shopIdArr){
            Record record = Db.findFirst("select id_ from t_prod_rec where rec_id_ = ? and src_type_ = ?"
                    ,id,"indexShop");
            if(record != null){
                Db.delete("t_prod_rec","id_",record);
            }
        }

        renderJson(new JsonResult().status("000").show("取消推荐成功"));
    }
}
